<template>
  <el-aside :width="$store.state.isCollapse ? '180px':'64px'">
    <el-menu
        active-text-color="#ffd04b"
        background-color="#545c64"
        class="el-menu-vertical-demo"
        text-color="#fff"
        :collapse="!$store.state.isCollapse"
        :collapse-transition="false"
    >
      <h3 v-show="$store.state.isCollapse">高校服务系统</h3>
      <h3 v-show="!$store.state.isCollapse">高校</h3>

      <el-menu-item
          v-for="item in noChildren()"
          :index="item.path"
          :key="item.path"
          @click="clickMenu(item)"
      >
        <component class="icons" :is="item.icon"></component>
        <span>{{ item.label }}</span>
      </el-menu-item>


      <el-sub-menu v-for="item in hasChildren()" :index="item.label" :key="item.path">
        <template #title>
          <component class="icons" :is="item.icon"></component>
          <span>{{ item.label }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item
              v-for="(subItem,subIndex) in item.children"
              :index="subItem.path"
              :key="subIndex"
              @click="clickMenu(subItem)"
          >
            <component class="icons" :is="subItem.icon"></component>
            <span>{{ subItem.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>
    </el-menu>
  </el-aside>
</template>

<script>
import {useRouter} from "vue-router";
import {useStore} from "vuex";

export default {
  name: "CommonAside",
  setup() {
    const store = useStore()
    const router = useRouter();
    const list = [
      {
        path: "/user",
        name: "UserHome",
        label: "用户管理",
        icon: "user",
        url: "UserManage/UserManage",
      },
      {
        label: "其他",
        icon: "location",
        path: "/other",
        children: [
          {
            path: "/page1",
            name: "Page1Home",
            label: "页面1",
            icon: "setting",
            url: "Other/PageOne",
          },
          {
            path: "/page2",
            name: "Page2Home",
            label: "页面2",
            icon: "setting",
            url: "Other/PageTwo",
          },
        ],
      },
      {
        label: "学生管理",
        icon: "Sunrise",
        path: "/student",
        children: [
          {
            path: "/student/query",
            name: "StudentQuery",
            label: "学生查询",
            icon: "setting",
            url: "",
          },
          {
            path: "/student/edit",
            name: "StudentDivide",
            label: "学生分班",
            icon: "setting",
            url: "",
          }, {
            path: "/student/import",
            name: "StudentImport",
            label: "导入新生",
            icon: "setting",
            url: "",
          },
        ],
      },
      {
        label: "系统管理",
        icon: "Setting",
        path: "/system",
        children: [
          {
            path: "/system/college",
            name: "CollegeManagement",
            label: "学院管理",
            icon: "setting",
            url: "",
          },
          {
            path: "/system/specialty",
            name: "SpecialtyManagement",
            label: "专业管理",
            icon: "setting",
            url: "",
          },
          {
            path: "/system/user",
            name: "UserManagement",
            label: "用户管理",
            icon: "setting",
            url: "",
          },
        ],
      },
    ];
    //过滤处理list中的数据
    const noChildren = () => {
      return list.filter((item) => !item.children);
    }
    //过滤处理list中的数据
    const hasChildren = () => {
      return list.filter((item) => item.children);
    }

    //点击菜单实现跳转
    //根据store/index.js中配置的路由的组件名称跳转
    const clickMenu = (item) => {
      router.push({
        name: item.name,
      });

      //面包屑功能，使用vuex来实现
      store.commit('selectMenu', item);
    };

    return {
      noChildren,
      hasChildren,
      clickMenu
    }
  }
}

</script>

<style lang="less" scoped>
.icons {
  width: 18px;
  height: 18px;
}

.el-menu {
  border-right: none;

  h3 {
    line-height: 48px;
    color: #fff;
    text-align: center;
  }
}
</style>